<?php

/*
Process Add Pet (admin_edit_pet.pro.php)
*/

$rank_check = "7";
include "../global.inc.php";

$game=$_GET['game'];
$edit=$_GET['edit'];
$add_pet_name = $_POST['add_pet_name'];
$pet_desc = $_POST['pet_desc'];
$food_likes = $_POST['food_likes'];
$food_dislikes = $_POST['food_dislikes'];
$starter_pet = $_POST['starter_pet'];


$explodeAttacks = explode(", ", $food_likes);
$countAttacks = count($explodeAttacks) - 1;

if ($countAttacks != "0")
{
for ($a = 0; $a <= $countAttacks;)
{
	$findItem = fetch("SELECT * FROM items2 WHERE item_name = '$explodeAttacks[$a]' AND game = '$game'");
	if ((!$findItem[id]) AND ($food_likes))
	{
		die(header(error("admin_edit_pet.php?game=$game","The item \"$explodeAttacks[$a]\" does not exsist!")));
	}
	$a++;
}
}
$explodeAttacks = explode(", ", $food_dislikes);
$countAttacks = count($explodeAttacks) - 1;

if ($countAttacks != "0")
{
for ($a = 0; $a <= $countAttacks;)
{
	$findItem = fetch("SELECT * FROM items2 WHERE item_name = '$explodeAttacks[$a]' AND game = '$game'");
	if ((!$findItem[id]) AND ($food_dislikes))
	{
		die(header(error("admin_edit_pet.php?game=$game","The item \"$explodeAttacks[$a]\" does not exsist!")));
	}
	$a++;
}
}
if ($HTTP_POST_FILES['petImage']['size'] >= 1)
{
	$directoryName = "../images/user_images/opg_$game";
	if (!file_exists($directoryName)) { mkdir($directoryName, 0777); }
	
	$directoryName2 = 	"../images/user_images/opg_$game/pets";
	if (!file_exists($directoryName2)) { mkdir($directoryName2, 0777); }
	
	if ($HTTP_POST_FILES['petImage']['size'] >= 32000)
	{
		die(header(error("admin_edit_pet.php?game=$game", "Your image must be under 32,000 bytes (or 	32kb)")));
	}
	
	if (!eregi("image/", $HTTP_POST_FILES['petImage']['type']))
	{
		die(header(error("admin_edit_pet.php?game=$game", "You can only use .gif, .jpg, and .jpeg files 	for images. Please convert the image to one of these file types.")));
	}
}

$check = fetch("SELECT id FROM pets2 WHERE id = '$edit' AND game = '$game'");

if (!$check[id])
{
	die(header(error("admin_edit_pet.php?game=$game", "That pet doesn't exsist.")));
}
else
{

	mysql_query("UPDATE pets2 SET name = '$add_pet_name' WHERE id = '$edit' AND game = '$game'");
	mysql_query("UPDATE pets2 SET pet_desc = '$pet_desc' WHERE id = '$edit' AND game = '$game'");
	mysql_query("UPDATE pets2 SET item_likes = '$food_likes' WHERE id = '$edit' AND game = '$game'");
	mysql_query("UPDATE pets2 SET item_dislikes = '$food_dislikes' WHERE id = '$edit' AND game = '$game'");
	mysql_query("UPDATE pets2 SET starter = '$starter_pet' WHERE id = '$edit' AND game = '$game'");
	
	if ($HTTP_POST_FILES['petImage']['size'] >= 1)
	{
		$file = $HTTP_POST_FILES['petImage']['tmp_name'];
		$dest = 	"../images/user_images/opg_$game/pets/pet_$edit.gif";
		if (file_exists($dest)) { unlink($dest); }
		copy($file, $dest);
	}
	
	header("Location: admin_edit_pet.php?game=$game&error=The+pet+$add_pet_name+has+been+edited!");
}

?>
